Skip to content

fix: suppress PydanticJsonSchemaWarning for @tool parameters using Field(default_factory=...)#2065

Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/tool-field-default-factory-warning
Open

fix: suppress PydanticJsonSchemaWarning for @tool parameters using Field(default_factory=...)#2065
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/tool-field-default-factory-warning

Conversation

@Zelys-DFKH
Copy link
Copy Markdown

Description

When a @tool parameter uses Field(default_factory=list, description="items") as its default,
_extract_annotated_metadata in decorator.py passes the FieldInfo object itself as the
default= argument when constructing a new Field(). Since FieldInfo is not JSON serializable,
Pydantic emits a PydanticJsonSchemaWarning during schema generation:

PydanticJsonSchemaWarning: Default value annotation=NoneType required=False
default_factory=list description='items' is not JSON serializable;
excluding default from JSON schema

The fix checks whether param_default is a FieldInfo instance before building the output
field. When it is, default_factory or the scalar default is unwrapped from the FieldInfo
and forwarded to the new Field() call. The description attribute on the FieldInfo is also
now included in the tool schema when no docstring description is available.

Related Issues

Fixes #1914

Documentation PR

N/A

Type of Change

Bug fix

Testing

Added three tests to tests/strands/tools/test_decorator.py:

  • test_tool_field_default_factory_no_warning — verifies no PydanticJsonSchemaWarning is emitted for Field(default_factory=list, ...)

  • test_tool_field_default_factory_description_used — verifies the Field(description=...) value appears in the tool schema and the parameter is not required

  • test_tool_field_default_value_no_warning — verifies no warning for Field(default=5, ...)

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…y=...)

When a @tool parameter uses Field(default_factory=list) as its default,
_extract_annotated_metadata passed the FieldInfo object itself as the
`default=` argument to a new Field(), which triggered a
PydanticJsonSchemaWarning because FieldInfo is not JSON serializable.

Now when param_default is a FieldInfo, the default_factory or scalar
default is unwrapped and forwarded correctly. The description from the
Field() call is also surfaced in the tool schema.

Fixes strands-agents#1914
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] @tool emits PydanticJsonSchemaWarning for Field(default_factory=list)

1 participant